package application

import (
	"github.com/devfeel/dotweb"
	"git.oschina.net/YPTArch/appcenter/models"
	"strings"
	"fmt"
	"github.com/devfeel/dotweb/framework/crypto"
)
//
func CreateHeandler(ctx *dotweb.HttpContext)  {
	result := new(models.Result)
	defer ctx.WriteJson(result)
	app:=new(models.Application)
	ctx.Bind(app)
	if strings.TrimSpace(app.AppName) == "" {
		result.FailResult("应用名称不能为空")
		fmt.Println(result)
		return
	}
	app.AppId=cryptos.GetUUID()
	res,err:= models.ApplicationService.Create(app)
	if err !=nil{
		fmt.Println(err)
		result.ServerErrResult()
	}
	if !res{
		fmt.Println(err)
		result.FailResult("应用创建失败")
	}
	result.SuccessResult(nil)
}
//
func GetAppHeandler(ctx *dotweb.HttpContext)  {
	result := new(models.Result)
	defer ctx.WriteJson(result)
	appId:=ctx.GetRouterName("appId")
	app,err:= models.ApplicationService.GetByAppId(appId)
	if err !=nil{
		fmt.Println(err)
		result.ServerErrResult()
	}
	result.SuccessResult(app)
}

